import { Injectable } from '@nestjs/common';
import { OllamaService } from '../ollama/ollama.service';

export interface ModelInfo {
  id: string;
  name: string;
  description?: string;
  provider: string;
  size?: number;
}

@Injectable()
export class ModelsService {
  constructor(private readonly ollamaService: OllamaService) {}

  async getModels(): Promise<ModelInfo[]> {
    const ollamaModels = await this.ollamaService.getModels();

    return ollamaModels.map((model) => ({
      id: model.name,
      name: this.formatModelName(model.name),
      description: this.getModelDescription(model.name),
      provider: 'Ollama',
      size: model.size,
    }));
  }

  /**
   * 格式化模型名称
   */
  private formatModelName(name: string): string {
    // 将模型名称格式化为更友好的显示名称
    // 例如: "llama2:7b" -> "Llama 2 7B"
    return name
      .split(':')
      .map((part) => {
        // 处理数字和字母
        return part
          .replace(/([a-z])([A-Z])/g, '$1 $2')
          .replace(/(\d+)/g, ' $1 ')
          .trim();
      })
      .join(' ')
      .replace(/\s+/g, ' ')
      .trim();
  }

  /**
   * 获取模型描述
   */
  private getModelDescription(name: string): string {
    const descriptions: Record<string, string> = {
      llama2: 'Meta 的 Llama 2 模型',
      llama3: 'Meta 的 Llama 3 模型',
      mistral: 'Mistral AI 的高性能模型',
      codellama: '专门用于代码生成的模型',
      phi: 'Microsoft 的 Phi 模型',
      gemma: 'Google 的 Gemma 模型',
    };

    const key = name.toLowerCase().split(':')[0];
    return descriptions[key] || 'Ollama 模型';
  }
}

